package mq

import (
	"cloud-disk/global/define"
	amqp "github.com/rabbitmq/amqp091-go"
	"log"
	"os"
)

var conn *amqp.Connection
var channel *amqp.Channel

func InitMQ() {
	if !define.Conf.RabbitMQ.AsyncTransferEnable {
		return
	}
	ok := InitChannel()
	if !ok {
		os.Exit(1)
	}
}

func Channel() *amqp.Channel {
	return channel
}

func InitChannel() bool {
	conf := define.Conf.RabbitMQ
	if channel != nil {
		return true
	}
	var err error
	conn, err = amqp.Dial(conf.Url)
	if err != nil {
		log.Println("rabbitmq dial fail : " + err.Error())
		return false
	}
	channel, err = conn.Channel()
	if err != nil {
		log.Println("rabbitmq channel fail : " + err.Error())
		return false
	}
	return true
}
